﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
	public partial interface IService
{
[OperationContract]
string Area_Add(Area model ,int ACID);


[OperationContract]
string Area_Update(Area model ,int ACID);


[OperationContract]
string AreaByAreaCode_Delete(string AreaCode,int ACID);


[OperationContract]
Area AreaByAreaCode_Get(string AreaCode,int ACID );


[OperationContract]
List<Area> Area_Get(string sortBy,int ACID);




}
public partial class Service : IService
{
#region  添加Area表数据
/// <summary>
/// 添加Area
/// </summary>
/// <param name="model">Area实体</param>
/// <returns>空或错误</returns>
public string Area_Add(Area model,int ACID)
{
if(string.IsNullOrEmpty(model.AreaCode))
{
return "『地区编码』不能为空！";
}
if(string.IsNullOrEmpty(model.AreaName))
{
return "『地区名称』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.Area.Count(p=>p.AreaCode==model.AreaCode)>0)
{
return "主键已存在！";
}
if(ctx.Area.Count(p=>p.AreaName==model.AreaName)>0)
{
return "地区名称已存在！";
}
ctx.Area.InsertOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 添加失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  添加Area表数据事物用重构
/// <summary>
/// 添加Area事物用重构
/// </summary>
/// <param name="model">Area实体</param>
/// <returns>空或错误</returns>
private string Area_Add(Area model,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(string.IsNullOrEmpty(model.AreaCode))
{
return "『地区编码』不能为空！";
}
if(string.IsNullOrEmpty(model.AreaName))
{
return "『地区名称』不能为空！";
}
if(ctx.Area.Count(p=>p.AreaCode==model.AreaCode)>0)
{
return "主键已存在！";
}
if(ctx.Area.Count(p=>p.AreaName==model.AreaName)>0)
{
return "地区名称已存在！";
}
ctx.Area.InsertOnSubmit(model);
return string.Empty;
}
#endregion



#region  修改Area表数据
/// <summary>
/// 修改Area
/// </summary>
/// <param name="model">Area实体</param>
/// <returns>空或错误</returns>
public string Area_Update(Area model,int ACID)
{
if(string.IsNullOrEmpty(model.AreaCode))
{
return "『地区编码』不能为空！";
}
if(string.IsNullOrEmpty(model.AreaName))
{
return "『地区名称』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.Area.Count(p=>p.AreaCode==model.AreaCode)!= 1)
{
return "未找到指定记录或记录不唯一！";
}
Area Areamodel=ctx.Area.Single(p => p.AreaCode==model.AreaCode);
if(!string.IsNullOrEmpty(model.AreaCode))
{
Areamodel.AreaCode=model.AreaCode;
}
if(!string.IsNullOrEmpty(model.AreaName))
{
Areamodel.AreaName=model.AreaName;
}
if(!string.IsNullOrEmpty(model.ParentCode))
{
Areamodel.ParentCode=model.ParentCode;
}
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 修改失败!错误详细信息：" + ex.Message;
}
}
}
#endregion



#region  删除Area表ByAreaCode数据
/// <summary>
/// 删除AreaByAreaCode
/// </summary>
/// <param name="AreaCode">地区编码</param>
/// <returns>空或错误</returns>
public string AreaByAreaCode_Delete(string AreaCode,int ACID)
{
if(string.IsNullOrEmpty(AreaCode))
{
return "『地区编码』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.Area.Count(p=>p.AreaCode==AreaCode)!=1)
{
return null;
}
Area model=ctx.Area.Single(p => p.AreaCode== AreaCode);
ctx.Area.DeleteOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 删除失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  删除Area表ByAreaCode数据[事物用重构]
/// <summary>
/// 删除AreaByAreaCode[事物用重构]
/// </summary>
/// <param name="AreaCode">地区编码</param>
/// <returns>空或错误</returns>
private string AreaByAreaCode_Delete(string AreaCode,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(string.IsNullOrEmpty(AreaCode))
{
return "『地区编码』不能为空！";
}
if(ctx.Area.Count(p=>p.AreaCode==AreaCode)!=1)
{
return null;
}
Area model=ctx.Area.Single(p => p.AreaCode== AreaCode);
ctx.Area.DeleteOnSubmit(model);
return string.Empty;
}
#endregion



#region  获取Area表ByAreaCode数据
/// <summary>
/// 获取AreaByAreaCode
/// </summary>
/// <param name="AreaCode">地区编码</param>
/// <returns>当前表</returns>
public Area AreaByAreaCode_Get(string AreaCode,int ACID)
{
if(string.IsNullOrEmpty(AreaCode))
{
return null;
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.Area.Count(p=>p.AreaCode==AreaCode)!=1)
{
return null;
}
return ctx.Area.SingleOrDefault(p=>p.AreaCode==AreaCode);
}
catch (Exception ex)
{
return null;
}
}
}
#endregion



#region  获取Area表数据
/// <summary>
/// 获取Area
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<Area></returns>
public List<Area> Area_Get(Expression<Func<Area, bool>> predicate , string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.Area.ToList();
}
 else 
{
return ctx.Area.OrderBy(sortBy).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.Area.Where(predicate).ToList();
}
 else 
{
return ctx.Area.Where(predicate).OrderBy(sortBy).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  获取Area表数据
/// <summary>
/// 获取Area
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<Area></returns>
public List<Area> Area_Get(string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
return Area_Get(null,sortBy, ACID);
}
catch (Exception)
{
return null;
}
}
}
#endregion


#region  获取Area表数据计数
/// <summary>
/// 获取Area表数据计数
/// </summary>
/// <returns>List<Area></returns>
public int Area_GetCount(Expression<Func<Area, bool>> predicate,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
return ctx.Area.Count();

}
 else
{
return ctx.Area.Where(predicate).Count();
}
}
catch 
{
return 0;
}
}
}
#endregion




#region  获取Area分页表数据
/// <summary>
/// 获取Area分页表数据
/// </summary>
/// <param name="pageIndex">当前页</param>
/// <param name="pageSize">页大小</param>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<Area></returns>
public List<Area> Area_Getpage(int pageIndex, int pageSize,Expression<Func<Area, bool>> predicate ,string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.Area.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.Area.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.Area.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.Area.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  批量修改Area表数据
/// <summary>
/// 批量修改Area
/// </summary>
/// <param name="predicate">Expression<Func<Area, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<Area,Area>> 更新表达式树</param>
/// <param name="returns">影响记录数</param>
public int Area_batchUpdate(Expression<Func<Area, bool>> predicate,Expression<Func<Area, Area>> updater,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.Area.Update(predicate, updater);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量修改Area表数据事物用重构
/// <summary>
/// 批量修改Area事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<Area, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<Area,Area>> 更新表达式树</param>
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int Area_batchUpdate(Expression<Func<Area, bool>> predicate,Expression<Func<Area, Area>> updater,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.Area.Update(predicate, updater);
return ok;
}
#endregion


#region  批量删除Area表数据
/// <summary>
/// 批量删除Area
/// </summary>
/// <param name="predicate">Expression<Func<Area, bool>></param> 查找表达式树
/// <param name="returns">影响记录数</param>
public int Area_batchDel(Expression<Func<Area, bool>> predicate,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.Area.Delete(predicate);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量删除Area表数据事物用重构
/// <summary>
/// 批量删除Area事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<Area, bool>></param> 查找表达式树
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int Area_batchDel(Expression<Func<Area, bool>> predicate,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.Area.Delete(predicate);
return ok;
}
#endregion


}
}

